import com.service.ConnectionUtils.ConnectionFactory;
import java.io.IOException;

import java.sql.Connection;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Shravani
 */
public class RegistrationServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException 
    {
        int i=0;
        ConnectionFactory c=new ConnectionFactory();
        res.setContentType("text/html");         
        String Firstname=req.getParameter("First name");
//Statement st=null;
                
       String lastname =req.getParameter("last name");
       String Age=req.getParameter("Age");
       int Age1=Integer.parseInt(Age);
       String Gender=req.getParameter("Gender");
 
       String Address=req.getParameter("address");

      String Phonenumber1=req.getParameter("Phone number");

         long Phonenumber=Long.parseLong(Phonenumber1);
         String mail=req.getParameter("Email id");
        String uname1=req.getParameter("uname");
       String password=req.getParameter("password");
//      Connection con=ConnectionFactory.getConnection();

        
//      String sql="insert into UserDB values(?,?,?,?,?,?,?,?);";

//              st=con.prepareStatement(sql);
//              st.setString(1, Firstname);
//              st.setString(2,lastname);
//              st.setString(3, dob);
//              st.setString(4,Gender );
//              st.setString(5, Address);
//              st.setString(6, Course);
//              st.setInt(7, Phonenumber);
//              st.setString(8, mail);
//              
//      
try{
    
    Connection con=c.getConnection();
    Statement st=con.createStatement();
  String sql="insert into USERDB values('"+Firstname+"','"+lastname+"',"+Age1+",'"+Gender+"','"+Address+"',"+Phonenumber+",'"+mail+"','"+uname1+"','"+password+"')";
        //st=con.createStatement();
        i=st.executeUpdate(sql);
        //int i=0;
        st.close();
        con.close();
        } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        RequestDispatcher rd=req.getRequestDispatcher("/index.jsp");//null;  
 //req.getRequestDispatcher("/Stuent");
boolean b;
        
        if(i==1)
        {
                b=true;
        req.setAttribute("status", b);
        req.setAttribute("uname",uname1);
        
        }
        
        else
        {
         b=false;
        req.setAttribute("status", b);
        }
         rd.forward(req,res)    ;
        
        }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse 
response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HT
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
